$(function(){
    (function(){
        var str=$("#contpro").html().trim();
        $("textarea").val(str)
    })()

    var layer;
    layui.use('layer', function () {
        layer= layui.layer;
    });


    //调用Markdown编辑器
    var testEditor;

    testEditor = editormd("test-editormd", {
        width: "1100px",
        height: "680px",
        syncScrolling: "single",
        path: "/lib/",
        imageUpload: true,
        imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
        imageUploadURL: "/file",
        saveHTMLToTextarea: true
        // previewTheme : "dark"
    });

    // 更新文章
    $("#mudify").click(function(){
        opration("/articel/mudify/"+$(this).attr("data-id"),"post");
    })
    
    // 发表文章
    $("#mpublished").click(function () {
            opration("/articel/published","post");
    })

    function opration(path,type){
        const str=$("textarea")
                        .html().replace(/\*/g, "")
                        .replace(/\#/g,"")
                        .replace(/ /g,"")
                        .replace(/[\r\n]/g," ")
                        .replace(/\!\[]\((.+?)\)/g,"").slice(0,250)
        $.ajax({
            url: path,
            method:type,
            data: {
                author: $("#userid").val(),
                category: $(".mcat").val(),
                title: $(".mtitle").val(),
                content: $(".editormd-preview").html(),
                contentpro:$("textarea").html(),
                Introduction:str,
                date: new Date(),
            },
            success: function (msg) {
                layer.msg(msg.message, {
                    icon: 1,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                }, function () {
                    window.location.href = `/articel/Info/${msg.id}`;
                });
            }
        })
    }

    // 关闭Markdown编辑器
    $("#mclose").click(function () {
        window.location.href = "/usercenter"
    })

})